package com.caseprocessing.model;

import java.util.List;

/**
 * 文档分割片段
 * 表示一个独立的文书单元
 */
public class DocumentSegment {
    
    private String segmentId;
    private DocumentType documentType;
    private String title;
    private String documentNumber;
    private String responsibleParty;
    private String date;
    private int startPage;
    private int endPage;
    private List<Page> pages;
    private double confidence;  // 分割置信度
    
    public DocumentSegment() {}
    
    public DocumentSegment(String segmentId, int startPage, int endPage) {
        this.segmentId = segmentId;
        this.startPage = startPage;
        this.endPage = endPage;
    }
    
    // Getters and Setters
    public String getSegmentId() { return segmentId; }
    public void setSegmentId(String segmentId) { this.segmentId = segmentId; }
    
    public DocumentType getDocumentType() { return documentType; }
    public void setDocumentType(DocumentType documentType) { this.documentType = documentType; }
    
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    
    public String getDocumentNumber() { return documentNumber; }
    public void setDocumentNumber(String documentNumber) { this.documentNumber = documentNumber; }
    
    public String getResponsibleParty() { return responsibleParty; }
    public void setResponsibleParty(String responsibleParty) { this.responsibleParty = responsibleParty; }
    
    public String getDate() { return date; }
    public void setDate(String date) { this.date = date; }
    
    public int getStartPage() { return startPage; }
    public void setStartPage(int startPage) { this.startPage = startPage; }
    
    public int getEndPage() { return endPage; }
    public void setEndPage(int endPage) { this.endPage = endPage; }
    
    public List<Page> getPages() { return pages; }
    public void setPages(List<Page> pages) { this.pages = pages; }
    
    public double getConfidence() { return confidence; }
    public void setConfidence(double confidence) { this.confidence = confidence; }
    
    /**
     * 获取文档片段的页数
     */
    public int getPageCount() {
        return endPage - startPage + 1;
    }
    
    /**
     * 获取文档片段的页码范围描述
     */
    public String getPageRange() {
        if (startPage == endPage) {
            return String.valueOf(startPage);
        }
        return startPage + "-" + endPage;
    }
    
    /**
     * 获取文档片段的全部文本内容
     */
    public String getFullText() {
        if (pages == null || pages.isEmpty()) {
            return "";
        }
        
        StringBuilder fullText = new StringBuilder();
        for (Page page : pages) {
            fullText.append(page.getFullText()).append("\n");
        }
        return fullText.toString().trim();
    }
    
    /**
     * 检查文档片段是否包含指定关键词
     */
    public boolean containsKeyword(String keyword) {
        String fullText = getFullText().toLowerCase();
        return fullText.contains(keyword.toLowerCase());
    }
    
    /**
     * 检查文档片段是否有效（基本验证）
     */
    public boolean isValid() {
        return startPage > 0 && endPage >= startPage && 
               title != null && !title.trim().isEmpty();
    }
}